package com.ruoyi.file.controller;

import com.google.gson.Gson;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.file.service.ISysFileFormatService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * System file format controller.
 *
 * @author william.zhao
 * @version 1.0
 * @date 2024/06/16
 * @since jdk-17.0.2
 */
@Slf4j
@RestController()
public class SysFileFormatController {
    @Autowired
    private ISysFileFormatService formatService;
    /** for output the parameter. */
    private Gson gson = new Gson();

    /**
     * format to.
     */
    @PostMapping("formatto")
    public R<String> formatTo(@RequestParam("filePath") String filePath, @RequestParam("targetFormat") String targetFormat) {
        try {
            log.info("Format To Param-->{}, {}", filePath, targetFormat);
            String reviewPath = formatService.formatFile(filePath, targetFormat);
            log.info("formatted {} to {}", filePath, reviewPath);
            return R.ok(reviewPath);
        } catch (Exception e) {
            log.error("formatted fail! -->{}, {}", filePath, targetFormat);
            log.error(e.getMessage(), e);
            return R.fail(e.getMessage());
        }
    }

}
